package com.qf.java2201.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.github.pagehelper.PageInfo;
import com.qf.java2201.common.PageVo;
import com.qf.java2201.common.R;
import com.qf.java2201.dto.StoreExcelDto;
import com.qf.java2201.listener.WebStoreListener;
import com.qf.java2201.pojo.Store;
import com.qf.java2201.service.StoreService;
import com.qf.java2201.utils.OssUtil;
import com.qf.java2201.vo.PieVo;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/store")
//@CrossOrigin(origins = {"http://127.0.0.1:5500"}, allowCredentials = "true")
public class StoreController {

    @Autowired
    private StoreService storeService;

    @RequestMapping("/storesPageData")
    // 这里 的  方法内的  @RequestParam  不能省略    省略就无法获取参数了
    public PageVo<Store> getPageData(@RequestParam Map<String,Object> params){
        return storeService.getPageData(params);
    }


    @RequestMapping("/addStore")
    public R addStore(Store store){
        int result = storeService.addStore(store);
        R<Store> r = new R<>();
        if(result==1){
            r.setCode(0);
            r.setMsg("添加成功");
        }else{
            r.setCode(-1);
            r.setMsg("添加失败");
        }
        return r;
    }

    @RequestMapping("/upload")
    public R uploadPic(MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        InputStream inputStream = file.getInputStream();

        String path = OssUtil.uploadFile(inputStream,filename);

        R<String> r = new R();
        r.setCode(0);
        r.setMsg("上传成功");
        r.setData(path);
        return r;
    }

    @RequestMapping("/deleteStore")
    public R deleteStore(int collect_id) {
        int result = storeService.deleteStore(collect_id);
        R<Store> r = new R<>();
        if(result==1){
            r.setCode(0);
            r.setMsg("删除成功");
        }else{
            r.setCode(-1);
            r.setMsg("删除失败");
        }
        return r;
    }

    @RequestMapping("/updateStore")
    public R updateStudent(Store store) {
        int result = storeService.updateStore(store);
        R<Store> r = new R<>();
        if(result==1){
            r.setCode(0);
            r.setMsg("修改成功");
        }else{
            r.setCode(-1);
            r.setMsg("修改失败");
        }
        return r;
    }
    @RequestMapping("/selectStore")
    public R selectStore(Store store) {
        List<Store> result = storeService.selectStore(store);
        R r = new R();
        if(result != null){
            r.setCode(0);
            r.setData(result);
            r.setMsg("查询成功");
        }else{
            r.setCode(-1);
            r.setMsg("查询失败");
        }
        return r;
    }

    @RequestMapping("/echarts")
    public R echartsStore() {
        List<PieVo> list = storeService.echartsStore();

        R ok = R.ok();
        ok.setData(list);
        return ok;
    }

    @RequestMapping("/export")
    public void export(Store store, HttpServletResponse response) throws IOException {
        // 导出学生数据
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //防止中文乱码
        String filename = URLEncoder.encode("收藏信息导出", "utf-8");

        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");

        ServletOutputStream outputStream = response.getOutputStream();

        ExcelWriterBuilder write = EasyExcel.write(outputStream,Store.class);

        ExcelWriterSheetBuilder sheet = write.sheet();

        //准备列表数据
//        PageInfo<Store> pageData = storeService.getPageData(1, new Store());
        List<Store> stores = storeService.selectStoreList(store);

        sheet.doWrite(stores);
    }

    @RequestMapping("/templateDownload")
    public void templateDownload(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 下载  模板
        // 设置响应数据的类型
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");

        // 读取服务器的模板文件 转为 字节数组
        String path = request.getServletContext().getRealPath("/template/收藏信息模板.xlsx");
        File file = new File(path);
        //把文件对象 转为 字节数组对象
        byte[] bytes = FileUtils.readFileToByteArray(file);

        //防止中文乱码
        String filename = URLEncoder.encode("收藏信息模板", "utf-8");

        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");

        ServletOutputStream outputStream = response.getOutputStream();

        outputStream.write(bytes);

        outputStream.close();
    }

    @Autowired
    private WebStoreListener webStoreListener;

    @RequestMapping("/import")
    public R importExcelData(MultipartFile file) throws IOException {
        // 上传 excel 表格

        ExcelReaderBuilder read = EasyExcel.read(file.getInputStream(), StoreExcelDto.class, webStoreListener);

        ExcelReaderSheetBuilder sheet = read.sheet();

        sheet.doRead();

        return  R.ok();
    }
}
